/*
 * Copyright 2010 Okan Kahraman
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package tr.org.tx.vmot.ui.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;

import tr.org.tx.vmot.ui.base.AbstractView;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.Notification;

/**
 * @author Okan Kahraman
 *
 */
public class LoginView extends AbstractView {

	private static final long serialVersionUID = 4713742889274595096L;
	
	@Autowired
	private transient AuthenticationManager authenticationManager;
	
	public static class LoginEvent extends Event {
		
		private static final long serialVersionUID = -38139096742409945L;
		
		private final Authentication authentication;

		public LoginEvent(LoginView source, Authentication authentication) {
			super(source);
			this.authentication = authentication;
		}
		
		public Authentication getAuthentication() {
			return authentication;
		}
		
	}
	
	public LoginView() {
		constructView();
	}

	
	private void constructView() {
		final Panel loginPanel = new Panel();
		loginPanel.setCaption(getMessage("login.title"));
		((VerticalLayout) loginPanel.getContent()).setSpacing(true);

		final TextField username = new TextField(getMessage(
				"login.username"));
		username.setWidth("100%");
		loginPanel.addComponent(username);

		final TextField password = new TextField(getMessage(
				"login.password"));
		password.setSecret(true);
		password.setWidth("100%");
		loginPanel.addComponent(password);

		final Button loginButton = new Button(getMessage(
				"login.button"));
		//loginButton.setStyleName("primary");
		// TODO Make it possible to submit the form by pressing <Enter> in any
		// of the text fields
		loginPanel.addComponent(loginButton);
		((VerticalLayout) loginPanel.getContent()).setComponentAlignment(
				loginButton, Alignment.MIDDLE_RIGHT);
		loginButton.addListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				final Authentication auth = new UsernamePasswordAuthenticationToken(
						username.getValue(), password.getValue());
				try {
					
					Authentication returned = authenticationManager
							.authenticate(auth);
					
					fireEvent(new LoginEvent(LoginView.this, returned));
				} catch (BadCredentialsException e) {
					
					getWindow().showNotification(
							getMessage(
									"login.badCredentials.title"),
							getMessage(
									"login.badCredentials.descr"),
							Notification.TYPE_WARNING_MESSAGE);
				} catch (DisabledException e) {
					getWindow()
							.showNotification(
									getMessage(
											"login.disabled.title"),
									getMessage(
											"login.disabled.descr"),
									Notification.TYPE_WARNING_MESSAGE);
				} catch (LockedException e) {
					getWindow().showNotification(
							getMessage("login.locked.title"),
							getMessage("login.locked.descr"),
							Notification.TYPE_WARNING_MESSAGE);
				} catch (Exception e) {
					
					//ExceptionUtils.handleException(getWindow(), e);
				}
			}
		});

		loginPanel.setWidth("300px");

		final HorizontalLayout viewLayout = new HorizontalLayout();
		viewLayout.addComponent(loginPanel);
		viewLayout.setComponentAlignment(loginPanel, Alignment.MIDDLE_CENTER);
		viewLayout.setSizeFull();
		viewLayout.setMargin(true);

		setCompositionRoot(viewLayout);
		setSizeFull();
		
	}
	
	
	
	

}
